package com.qyl.honghengshop.controller.customer;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.qyl.honghengshop.common.PageConstant;
import com.qyl.honghengshop.dto.Comment;
import com.qyl.honghengshop.dto.Goods;
import com.qyl.honghengshop.dto.User;
import com.qyl.honghengshop.service.CommentService;
import com.qyl.honghengshop.service.GoodsService;
import com.qyl.honghengshop.service.impl.CommentServiceImpl;
import com.qyl.honghengshop.service.impl.GoodsServiceImpl;
import com.qyl.honghengshop.utils.FileUpload;

@Controller
@RequestMapping("/customer/comment")
public class CommentController {

	private CommentService commentService = new CommentServiceImpl();
	private GoodsService goodsService = new GoodsServiceImpl();
	
	@RequestMapping("/toAddCommon")
	public String toAddCommon(Integer id,ModelMap map){
		
		Goods goods = goodsService.getShopById(id);
		
		map.put("goods", goods);
		
		return "evaluation_page";
	}
	
	@RequestMapping("/addComment")
	public String addComment(Comment comment,MultipartFile goodsImg,HttpSession session,ModelMap map) throws Exception{
		
		User user = (User) session.getAttribute(PageConstant.SESSION_USER);
		
		String fileName = FileUpload.fileUpload(goodsImg);
		
		comment.setImage(fileName);
		
		comment.setCreate_date(new Date());
		
		comment.setUser(user);
		
		int count = commentService.addComment(comment);
		
		if(count > 0){
			map.put(PageConstant.TIP, "添加评论成功");
			map.put("comment", comment);
		}else{
			map.put(PageConstant.TIP, "添加评论失败");
		}
		
		return "evaluation_page";
	}
	@RequestMapping("/getCommentList")
	@ResponseBody
	public List<Comment> getCommentList(ModelMap map,Integer id){
		
		List<Comment> commentList = commentService.getCommentList(id);
		
		map.put("commentList", commentList);
		
		return commentList;
	}
}
